package com.telephone.purchase.controller;

import com.telephone.purchase.entity.*;
import com.telephone.purchase.entity.Package;
import com.telephone.purchase.mapper.ClassifyMapper;
import com.telephone.purchase.service.*;
import com.telephone.purchase.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/good")
public class GoodController {
    @Autowired
    private GoodService goodService;
    @Autowired
    private PackageService packageService;
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/selectGood")
    public Result selectGood() {
        try {
            List<Goods> list = goodService.selectGood();
            if (list != null) {
                System.out.println(list);
                return Result.success(list);
            } else {
                return Result.fail("9001", "获取数据发生异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("9001", "获取数据发生异常");
        }
    }

    @RequestMapping("/selectByGoodId")
    public Result selectByGoodId(Long goodId) {
        try {
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            Map<String, Object> map = new HashMap<>();
            Good good = goodService.selectByPrimaryKey(goodId);
            List<Package> packageList = packageService.selectByGoodId(goodId);
            map.put("package", packageList);
            map.put("good", good);
            mapList.add(map);

            if (mapList != null) {
                System.out.println(good);
                System.out.println(packageList);
                return Result.success(mapList);
            } else {
                return Result.fail("9001", "获取数据发生异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("9001", "获取数据发生异常");
        }
    }

    @RequestMapping("/selectGoodByTime")
    public Result selectGoodByTime() {
        try {
            List<Goods> list = goodService.selectGoodByTime();
            if (list != null) {
                System.out.println(list);
                return Result.success(list);
            } else {
                return Result.fail("9001", "获取数据发生异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("9001", "获取数据发生异常");
        }
    }

    @RequestMapping("/selectGoodClass")
    public Result selectGoodClass() {
        try {
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            List<Classify> classifies = classifyService.selectAllClassify();
            for (Classify classify : classifies
            ) {
                Map<String, Object> map = new HashMap<>();
                map.put("classify_id", classify.getClassifyId());
                map.put("classify_name", classify.getClassifyName());
                List<Goods> list = goodService.selectGoodClass(classify.getClassifyId());
                map.put("goods", list);
                mapList.add(map);
            }

            if (mapList != null) {
                System.out.println(mapList);
                return Result.success(mapList);
            } else {
                return Result.fail("9001", "获取数据发生异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("9001", "获取数据发生异常");
        }
    }

    @RequestMapping("/updatePay")
    public Result updatePay(Long userId, Long goodId, Long packageId, String payPassword, BigDecimal payMoney) {
        try {
            Users users = usersService.selectByPrimaryKey(userId);
            Orders orders = new Orders();
            Package apackage = packageService.selectByPrimaryKey(packageId);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
            String currDate = dateFormat.format(new Date());

            if (users.getRoleId() == 0) {
                if (goodId == 0) {
                    if (payPassword.equals(users.getPayPassword())) {
                        if (payMoney.compareTo(users.getUserBalance()) == 1) {//返回值 -1 小于 0 等于 1 大于
                            return Result.fail("100002", "余额不足，请充值");
                        }
                        users.setUserBalance(users.getUserBalance().subtract(payMoney));
                        users.setRoleId(1);
                        usersService.updateByPrimaryKeySelective(users);

                        orders.setOrderNumber(currDate);
                        orders.setUserId(userId);
                        orders.setUserName(users.getUserName());
                        orders.setPhoneNumber(users.getPhoneNumber());
                        orders.setPackageId(packageId);
                        orders.setPackageName(apackage.getPackageName());
                        orders.setPackagePrice(apackage.getPackagePrice());
                        orders.setPayMoney(payMoney);
                        orders.setOrderResult("yes");
                        ordersService.insertSelective(orders);

                    } else {
                        return Result.fail("100001", "密码错误，支付失败");
                    }
                }
                if (payPassword.equals(users.getPayPassword())) {
                    if (payMoney.compareTo(users.getUserBalance()) == 1) {//返回值 -1 小于 0 等于 1 大于
                        return Result.fail("100002", "余额不足，请充值");
                    }
                    users.setUserBalance(users.getUserBalance().subtract(payMoney));
                    usersService.updateByPrimaryKeySelective(users);
                    orders.setOrderNumber(currDate);

                    orders.setUserId(userId);
                    orders.setUserName(users.getUserName());
                    orders.setPhoneNumber(users.getPhoneNumber());
                    orders.setPackageId(packageId);
                    orders.setPackageName(apackage.getPackageName());
                    orders.setPackagePrice(apackage.getPackagePrice());
                    orders.setPayMoney(payMoney);
                    orders.setOrderResult("yes");
                    ordersService.insertSelective(orders);
                } else {
                    return Result.fail("100001", "密码错误，支付失败");
                }

            } else if (users.getRoleId() == 1) {
                if (payPassword.equals(users.getPayPassword())) {
                    if (payMoney.compareTo(users.getUserBalance()) == 1) {//返回值 -1 小于 0 等于 1 大于
                        return Result.fail("100002", "余额不足，请充值");
                    }
                    users.setUserBalance(users.getUserBalance().subtract(payMoney));
                    usersService.updateByPrimaryKeySelective(users);

                    orders.setOrderNumber(currDate);
                    orders.setUserId(userId);
                    orders.setUserName(users.getUserName());
                    orders.setPhoneNumber(users.getPhoneNumber());
                    orders.setPackageId(packageId);
                    orders.setPackageName(apackage.getPackageName());
                    orders.setPackagePrice(apackage.getPackagePrice());
                    orders.setPayMoney(apackage.getPackageVipPrice());
                    orders.setOrderResult("yes");
                    ordersService.insertSelective(orders);

                } else {
                    return Result.fail("100001", "密码错误，支付失败");
                }
            }
            return Result.success("支付成功");

        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("9001", "获取数据发生异常");
        }

    }

}
